package com.znf4.notify.service.impl;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.znf4.id.IdGenerator;
import com.znf4.notify.entity.RpNotifyRecord;
import com.znf4.notify.enums.NotifyStatusEnum;
import com.znf4.notify.enums.NotifyTypeEnum;
import com.znf4.notify.service.RpNotifyService;

/**
 * 通知服务接口实现
 */
@Service("rpNotifyService")
public class RpNotifyServiceImpl implements RpNotifyService {

    private static final Logger LOG = LoggerFactory.getLogger(RpNotifyServiceImpl.class);


    @Autowired
    private JmsTemplate orderQueryJmsTemplate;

    
    
    @Autowired
    private IdGenerator idGenerator;





    /**
     * 发送快捷支付结果查询消息
     *
     * @param proxyPayMsg     快捷支付结果消息
     * @param merchantOrderNo 商户订单号
     * @param merchantNo      商户号
     */
    public void quickPayQueryNotifySend(String proxyPayMsg, String merchantOrderNo, String merchantNo) {
        LOG.info("发起MQ快捷支付查询开始 ,proxyPayMsg {},  merchantOrderNo {},  merchantNo{} " ,proxyPayMsg ,  merchantOrderNo,  merchantNo);

        RpNotifyRecord record = new RpNotifyRecord();
        record.setId(idGenerator.getNotifyRecordId()+"");
        record.setNotifyTimes(0);
        record.setLimitNotifyTimes(5);
//        record.setStatus(NotifyStatusEnum.CREATED.name());
        record.setUrl(proxyPayMsg);
        record.setMerchantOrderNo(merchantOrderNo);
        record.setMerchantNo(merchantNo);
        record.setNotifyType(NotifyTypeEnum.QUICK_PAY_RESULT_SEARCH.name());

        LOG.info("转换数据开始");
        Object toJSON = JSONObject.toJSON(record);
        final String str = toJSON.toString();
        LOG.info("转换数据结束");
        try {
        	orderQueryJmsTemplate.send(new MessageCreator() {
                public Message createMessage(Session session) throws JMSException {
                    return session.createTextMessage(str);
                }
            });
        }catch (Exception e){
            LOG.error("快捷支付结果查询发起异常:" , e);
        }
        LOG.info("发起MQ快捷支付查询结束");
    }


}
